package com.tboss.template.system.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import com.tboss.global.utils.useful.SecurityUtils;
import com.tboss.template.common.dto.NavigationDto;
import com.tboss.template.common.utils.DateUtils;
import com.tboss.template.system.dao.CodeTypeDao;
import com.tboss.template.system.dto.CodeTypeDto;

@Service
public class CodeTypeService {

	@Autowired SystemService systemService;
	@Autowired private CodeTypeDao cdTpDao;
	
	private Integer getCodeTypeListCnt(CodeTypeDto dto){
		return this.cdTpDao.getCodeTypeListCnt(dto);
	}
	
	private List<CodeTypeDto> getCodeTypeList(CodeTypeDto dto){
		return this.cdTpDao.getCodeTypeList(dto);
	}
	
	public void setModelCodeTypeList(Integer reqPage, Model model, CodeTypeDto dto){
		if(reqPage != null) dto.setCurrentPage(reqPage);
		
		Integer rowsCnt = this.getCodeTypeListCnt(dto);
		NavigationDto pageNavi = new NavigationDto(rowsCnt, dto.getRowPerPage(), dto.getCurrentPage());

		model.addAttribute("sysBase", this.systemService.getSysBase(dto));
		model.addAttribute("pageNavi", pageNavi);
		model.addAttribute("cdTpList", this.getCodeTypeList(dto));
	}
	
	public void getCodeTypeRegForm(Model model){
	}

	public void getCodeTypeRegForm(Model model, String cdTpId){
		model.addAttribute("cdTpInfo", this.cdTpDao.getCodeTypeInfo(cdTpId));
	}

	public void addCodeTypeInfo(CodeTypeDto dto){
		dto.setCdTpRegId(SecurityUtils.getCurrentUserName());
		dto.setCdTpRegDate(DateUtils.getDateFormat("yyyyMMddHHmmss"));
		this.cdTpDao.addCodeTypeInfo(dto);
	}
	
	public void updateCodeTypeInfo(CodeTypeDto dto){
		dto.setCdTpModId(SecurityUtils.getCurrentUserName());
		dto.setCdTpModDate(DateUtils.getDateFormat("yyyyMMddHHmmss"));
		this.cdTpDao.updateCodeTypeInfo(dto);
	}
	
	public void deleteCodeTypeInfo(CodeTypeDto dto){
		dto.setCdTpModId(SecurityUtils.getCurrentUserName());
		dto.setCdTpModDate(DateUtils.getDateFormat("yyyyMMddHHmmss"));
		
		this.cdTpDao.deleteCodeTypeInfo(dto);
	}
	
}
